package com.qzxy.premiumhotelsystem.config;

import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import com.qzxy.premiumhotelsystem.domain.entity.Admin;
import com.qzxy.premiumhotelsystem.domain.entity.Employees;
import com.qzxy.premiumhotelsystem.domain.entity.Guests;
import com.qzxy.premiumhotelsystem.service.AdminService;
import com.qzxy.premiumhotelsystem.service.EmployeesService;
import com.qzxy.premiumhotelsystem.service.GuestsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 李佳添
 * @version 1.0
 * @description 自定义角色
 * @date 2024/9/17 10:06 周二
 */
@Component
public class CustomStpInterface implements StpInterface {
    @Autowired
    private AdminService adminService;

    @Autowired
    private EmployeesService employeesService;

    @Autowired
    private GuestsService guestsService;

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        String loginIdStr = loginId.toString();
//        try {
//            loginIdStr = loginId.toString();
//            // 根据 id 获取角色列表
//        } catch (NumberFormatException e) {
//            System.err.println("无效的 loginId: " + loginIdStr);
//            // 处理异常，例如返回一个空的角色列表或抛出自定义异常
//            return Collections.emptyList();
//        }
        // 根据 loginId 和 loginType 查询用户角色
        return getRolesByUserId(loginIdStr, loginType);
    }

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        // 如果不使用权限表，可以返回空列表
//        return getPermissionsByUserId(loginId.toString(), loginType);
        return Collections.emptyList();
    }



    // 通过切换为guest用户，进行登录
    // StpUtil.setStpLogic(guestStpLogic);
    // 但只有一个StpLogicJwtForSimple()，无法使用new SaLoginModel().setExtra("ids",employees.getEmployeeId()
    // 存储更多的数据。
    public List<String> getRolesByUserId(String userId, String userType) {
        List<String> roles = new ArrayList<>();
        // 哭笑不得，果然天无绝人之路
        // token存储角色信息
        String role = StpUtil.getExtra("role").toString();
        switch (role) {
            case "admin":
                Admin admin = adminService.getByUsername(userId);
                if (admin != null) {
                    roles.add("admin");
                }
                break;
            case "employee":
                Employees employee = employeesService.getByTelephone(userId);
                if (employee != null) {
                    roles.add("employee");
                }
                break;
            case "guest":
                Guests guest = guestsService.getByTelephone(userId);
                if (guest != null) {
                    roles.add("guest");
                }
                break;
        }
        return roles;
    }


//    user:add
//    user:delete
//    user:update
//    user:view

    public List<String> getPermissionsByUserId(String userId, String userType) {
        List<String> permissions = new ArrayList<>();

        switch (StpUtil.getExtra("role").toString()) {
            case "admin":
                Admin admin = adminService.getByUsername(userId);
                if (admin != null) {
                    permissions.add("admin");
                }
                break;
            case "employee":
                Employees employee = employeesService.getByTelephone(userId);
                if (employee != null) {
                    permissions.add("employee");
                }
                break;
            case "guest":
                Guests guest = guestsService.getByTelephone(userId);
                if (guest != null) {
                    permissions.add("guest");
                }
                break;
        }
        System.out.println("userId:" + userId + ",userType:" + userType + ",permissions::" + permissions);
        return permissions;
    }
}



